/**
 * @module ChiVoxSDK
 * @author Don Li <kai.li@chivox.com>
 * @licence
 * Copyright (c) 2017 - 2020 ChiVox Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import Utils from './utils';
import cfg from './config.json';

/**
 * ControlPanel类。<br/><br/>
 *
 * ChiVoxSDK初始化时，录音机和播放器所需的控制面板。<br/>
 * 有播放，停止播放，录音，停止录音，回放录音，停止回放等功能。(仅用于控制UI控件状态)
 */
class ControlPanel {
    constructor() {
        this.ele_records = Utils.find_all(document, cfg.selector.panel.record) || [];
        this.ele_plays   = Utils.find_all(document, cfg.selector.panel.play) || [];
        this.ele_replays = Utils.find_all(document, cfg.selector.panel.replay) || [];
    }

    /**
     * 停止播放。
     * @param {Object=} curr_ele - 要停止播放的HTML DOM节点。当前页面有多个控制面板时需要指定。
     */
    playOff(curr_ele) {
        if (curr_ele) {
            Utils.removeClass(curr_ele, 'playOn');
            Utils.addClass(curr_ele, 'playOff');
        } else {
            for (var i = 0; i < this.ele_plays.length; i++) {
                Utils.removeClass(this.ele_plays[i], 'playOn');
                Utils.addClass(this.ele_plays[i], 'playOff');
            }
        }
    }

    /**
     * 开始播放。
     * @param {Object=} curr_ele - 要开始播放的HTML DOM节点。当前页面有多个控制面板时需要指定。
     */
    playOn(curr_ele) {
        console.info('playOn', curr_ele);
        if (!curr_ele && this.ele_plays.length > 0) {
            curr_ele = this.ele_plays[0];
        }

        if (curr_ele) {
            Utils.removeClass(curr_ele, 'playOff');
            Utils.addClass(curr_ele, 'playOn');
        }
    }

    /**
     * 停止录音。
     * @param {Object=} curr_ele - 要停止录音的HTML DOM节点。当前页面有多个控制面板时需要指定。
     */
    recordOff(curr_ele) {
        if (curr_ele) {
            Utils.removeClass(curr_ele, 'recordOn');
            Utils.addClass(curr_ele, 'recordOff');
        } else {
            for (var i = 0; i < this.ele_records.length; i++) {
                Utils.removeClass(this.ele_records[i], 'recordOn');
                Utils.addClass(this.ele_records[i], 'recordOff');
            }
        }
    }

    /**
     * 开始录音。
     * @param {Object=} curr_ele - 要开始录音的HTML DOM节点。当前页面有多个控制面板时需要指定。
     */
    recordOn(curr_ele) {
        if (!curr_ele && this.ele_records.length > 0) {
            curr_ele = this.ele_records[0];
        }

        if (curr_ele) {
            Utils.removeClass(curr_ele, 'recordOff');
            Utils.addClass(curr_ele, 'recordOn');
        }
    }

    /**
     * 禁用回放功能。
     * @param {Object=} curr_ele - 要禁止回放的HTML DOM节点。当前页面有多个控制面板时需要指定。
     */
    replayDisabled(curr_ele) {
        if (curr_ele) {
            Utils.removeClass(curr_ele, 'replayOn');
            Utils.removeClass(curr_ele, 'replayOff');
            Utils.addClass(curr_ele, 'replayDisabled');

            curr_ele.setAttribute('dataRecordId', '');
        } else {
            for (var i = 0; i < this.ele_replays.length; i++) {
                curr_ele = this.ele_replays[i];
                Utils.removeClass(curr_ele, 'replayOn');
                Utils.removeClass(curr_ele, 'replayOff');
                Utils.addClass(curr_ele, 'replayDisabled');

                curr_ele.setAttribute('dataRecordId', '');
            }
        }
    }

    /**
     * 停止回放功能。
     * @param {Object=} curr_ele - 要停止回放的HTML DOM节点。当前页面有多个控制面板时需要指定。
     */
    replayOff(curr_ele) {
        if (curr_ele) {
            Utils.removeClass(curr_ele, 'replayOn');
            Utils.removeClass(curr_ele, 'replayDisabled');
            Utils.addClass(curr_ele, 'replayOff');
        } else {
            for (var i = 0; i < this.ele_replays.length; i++) {
                curr_ele = this.ele_replays[i];
                Utils.removeClass(curr_ele, 'replayOn');
                Utils.removeClass(curr_ele, 'replayDisabled');
                Utils.addClass(curr_ele, 'replayOff');
            }
        }
    }

    /**
     * 开始回放功能。
     * @param {Object=} curr_ele - 要开始回放的HTML DOM节点。当前页面有多个控制面板时需要指定。
     */
    replayOn(curr_ele) {
        if (!curr_ele && this.ele_plays.length > 0) {
            curr_ele = this.ele_replays[0];
        }

        if (curr_ele) {
            Utils.removeClass(curr_ele, 'replayOff');
            Utils.removeClass(curr_ele, 'replayDisabled');
            Utils.addClass(curr_ele, 'replayOn');
        }
    }
}

export default ControlPanel
